<?php

namespace App\Http\Controllers\Admin;


use App\Models\Favorable;
use App\Models\FavorableUsed;

class FavorableController extends BaseController
{

    public function __construct(Favorable $favorable ,FavorableUsed $favorableUsed)
    {
        parent::__construct();
        $this->favorable = $favorable;
        $this->favorableused = $favorableUsed;
    }

    /**
     *后台:优惠券管理首页
     */
    public function getIndex()
    {
        $res = $this->favorable->favorableList(request());
        return view('admin.favorable.index',compact('res'));
    }

    public function getDuihuanList(){
        $res = $this->favorableused -> getDuihuanList(request());
        return view('admin.favorable.duihuanlist',compact('res'));
    }

    /**
     * 核销列表
     */
    public function getWriteOffList(){
        $res = $this->favorable->favorableList(request());
        return view('admin.favorable.write_off',compact('res'));
    }

    /**
     * 核销列表
     */
    public function getDelFavorableUsed(){
        $res = $this->favorableused->delFavorable(request());
        return $res;
    }

    /**
     * 核销
     */
    public function postWriteOff(){
         $res = $this->favorable->writeOff(request());
         return ajax_return($res);
    }

    /**
     *  新增优惠券
     */
    public function getAddFavorablezk(){
        if(request('id')){
              $data = $this->favorable->getFavorableInfo(request());
              return view('admin.favorable.addzk',compact('data'));
        }
        return view('admin.favorable.addzk');
    }

    /**
     *  新增优惠券
     */
    public function getAddFavorabledj(){
        if(request('id')){
            $data = $this->favorable->getFavorableInfo(request());
            return view('admin.favorable.adddj',compact('data'));
        }
        return view('admin.favorable.adddj');
    }

    /**
     *  新增优惠券
     */
    public function getAddFavorablety(){

        return view('admin.favorable.addty');
    }

    /**
     *  新增优惠券
     */
    public function postAddFavorable(){
          $res = $this->favorable->addFavorable(request());
          return redirect()->back()->with('message', $res)->withInput();
    }

    /**
     *  获取优惠券列表
     */
    public function getFavorable(){
         $res = $this->favorable->favorableList(request());
         if(request('type')==1){
             return view('admin.favorable.listzk',compact('res'));
         }else{
             return view('admin.favorable.listdj',compact('res'));
         }

    }

    /**
     *  删除优惠券
     */
    public function getDelFavorable(){
          $res = $this->favorable->delFavorable(request());
          return ajax_return($res);
    }

    /**
     * 已兑换优惠券
     */
    public function getFavoraableUsed(){
           $res = $this->favorableused->favorableUsedList();
           return view('admin.favorable.write_off',compact('res'));
    }
}
